package manager.Frame;

import com.sun.tools.javac.Main;
import manager.Frame.admin.AdminPanel;
import manager.Frame.user.UserPanel;
import manager.dao.UserDao;
import manager.pojo.Users;
import manager.util.SystemConstants;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.JPasswordField;
//登录面板
public class LoginPanel extends JPanel {
    //读取图片的路径的文件夹（images）
    private static final String DIR =LoginPanel.class.getClassLoader().getResource("manager/images").getPath();
    public LoginPanel(){
        this.setBounds(0,0, SystemConstants.FRAME_WIDTH,SystemConstants.FRAME_HEIGHT);
        this.setLayout(null);
        //创建一个面板，存放表单的背景图片和相关文本组件
        JPanel panel =new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2 =(Graphics2D) g;
                //绘制图片
                g2.drawImage(new ImageIcon(DIR+ "/arg.png").getImage(),0,0,getWidth(),getHeight(),null);
            }
        };
        //设置panel的大小和背景
        panel.setBounds(200,100,500,300);
        this.add(panel);

        //制作登录表单
        Box box =Box.createVerticalBox();
        panel.add(box);
        //创建一个垂直方向固定大小的盒子
        box.add(Box.createVerticalStrut(15));
        //创建标题的水平盒子
        Box box0=Box.createHorizontalBox();
        JLabel title=new JLabel("医疗管理系统");

        title.setFont(new Font("微软雅黑",Font.BOLD,20));
        box0.add(title);
        box.add(box0);
        //创建一个垂直方向固定大小的盒子
        box.add(Box.createVerticalStrut(20));
        //创建通用的边框格式和字体样式
        Font font =new Font("微软雅黑",Font.BOLD,20);
        Border border =BorderFactory.createLoweredSoftBevelBorder();
        //准备存放账号和输入框的盒子
        Box box1=Box.createHorizontalBox();
        JLabel nameLable=new JLabel("账号：");
        nameLable.setFont(font);
        box1.add(nameLable);
        //账号的输入框
        JTextField nameFiled=new JTextField(15);
        nameFiled.setBorder(border);
        box1.add(nameFiled);
        box.add(box1);
        box.add(Box.createVerticalStrut(15));

        //密码文字和密码输入框
        Box box2=Box.createHorizontalBox();
        JLabel pwdlable=new JLabel("密码：");
        pwdlable.setFont(font);
        box2.add(pwdlable);
        //账号的输入框
        JPasswordField pwdFiled= new JPasswordField(15);
        pwdFiled.setBorder(border);
        box2.add(pwdFiled);
        box.add(box2);
        box.add(Box.createVerticalStrut(15));
        //制作单选按钮，区分用户权限
        JRadioButton adminRadio =new JRadioButton("管理员",true);
        JRadioButton commonRadio =new JRadioButton("普通用户",false);

        //字体
        adminRadio.setFont(font);
        //设置为透明状态
        adminRadio.setOpaque(false);
        //选中时，radio是否有边框
        adminRadio.setFocusPainted(false);

        //字体
        commonRadio.setFont(font);
        //设置为透明状态
        commonRadio.setOpaque(false);
        //选中时，radio是否有边框
        commonRadio.setFocusPainted(false);

        //实现两个按钮二选一的效果
        ButtonGroup group =new ButtonGroup();
        group.add(adminRadio);
        group.add(commonRadio);
        //创建水平盒子，让两个按钮水平显示
        Box box3=Box.createHorizontalBox();
        box3.add(adminRadio);
        box3.add(commonRadio);
        box.add(box3);
        box.add(Box.createVerticalStrut(15));

        //登录按钮
        JButton loginBtn=new JButton("");
        Image login =new ImageIcon(DIR+"/log_btn.png").getImage().getScaledInstance(110,60,Image.SCALE_DEFAULT);
        //将图片加入到按钮中
        loginBtn.setIcon(new ImageIcon(login));
        //设置按钮不绘制边框
        loginBtn.setBorderPainted(false);
        //设置透明按钮
        loginBtn.setOpaque(false);
        //内容区域不设置填充
        loginBtn.setContentAreaFilled(false);
        //取消边框
        loginBtn.setBorder(null);

        //注册按钮
        JButton regBtn=new JButton("");
        Image reg =new ImageIcon(DIR+"/reg_btn.png").getImage().getScaledInstance(110,60,Image.SCALE_DEFAULT);
        //将图片加入到按钮中
        regBtn.setIcon(new ImageIcon(reg));
        //设置按钮不绘制边框
        regBtn.setBorderPainted(false);
        //设置透明按钮
        regBtn.setOpaque(false);
        //内容区域不设置填充
        regBtn.setContentAreaFilled(false);
        //取消边框
        regBtn.setBorder(null);


        //创建水平盒子————布局按钮
        Box box4=Box.createHorizontalBox();
        box4.add(loginBtn);
        box4.add(Box.createHorizontalStrut(20));
        box4.add(regBtn);
        box.add(box4);


        loginBtn.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                Users users=null;
                JPanel panel1=null;
                String username=nameFiled.getText();
                String password =new String(pwdFiled.getPassword());
                if(adminRadio.isSelected()){
                   users= new UserDao().getUserByName(username,1);
                   panel1=new AdminPanel();
                }else{
                    users=new UserDao().getUserByName(username,2);
                    panel1=new UserPanel();
                }
                //System.out.println(users);
                //没查到账号，不允许登录
                if(users==null|| !users.getPassword().equals(password)){
                    JOptionPane.showMessageDialog(loginBtn.getParent(),"用户名或密码输入错误","系统提示",JOptionPane.WARNING_MESSAGE);

                }
                else{
                    MainFrame.setContent(panel1);
                    MainFrame.users=users;

                }
            }
        });

    }
    //绘制方法，g画笔
    @Override
    protected void paintComponent(Graphics g){
        //将g转换成2d平面画笔
        Graphics2D g2 =(Graphics2D) g;
        //绘制图片
        //g2.drawImage(new ImageIcon(DIR+ "/bn.png").getImage(),0,0,getWidth(),getHeight(),null);
        //第二种方式
        try {
            Image image = ImageIO.read(new File(DIR + "/bn.png"));
            g2.drawImage(image,0,0,getWidth(),getHeight(),null);
        } catch(IOException e){
            throw new RuntimeException(e);
        }
    }
}
